<?php
$tb=array("drug","user","history","drug_history","hospital");
$tbn=3;
$table=$tb[$tbn];
function C($a){
	include "db2.php";
	return $db[$a];
}
$my_con=mysql_connect(C('DB_HOST').":".C('DB_PORT'),C('DB_USER'),C('DB_PWD'));
if(!$my_con){
		die('无法连接'.mysql_error());
	}
mysql_select_db(C('DB_NAME'),$my_con);
//取得表字段名
$query="SELECT * from ".C('DB_PREFIX').$table;
mysql_query("SET NAMES 'UTF8'");
$result=mysql_query($query,$my_con);
$arr="";
for($i=1;$i<mysql_num_fields($result);$i++){
	$ziduan=mysql_field_name($result,$i);
	$arr[$i]=$ziduan;
}
//将取得的字段名数组以,拼接为字符串
$zichuan=implode(",",$arr);
echo $zichuan."<br/>";
//建立初始数据

//取得使用药品人 和使用的药品清单
$query_people="SELECT * from ".C('DB_PREFIX').$tb[1];
mysql_query("SET NAMES 'UTF8'");
$result_people=mysql_query($query_people,$my_con);
$cos=mysql_num_rows($result_people);
$cosa=0;
$array2_people=array();
while($property=mysql_fetch_row($result_people)){
	$array2_people[$cosa]=$property[1];
	$cosa=$cosa+1;
}
//使用的药品及其单位
$query_drug="SELECT * from ".C('DB_PREFIX').$tb[0];
mysql_query("SET NAMES 'UTF8'");
$result_drug=mysql_query($query_drug,$my_con);
$cos=mysql_num_rows($result_drug);
$cosa_drug=0;
$array2_drug=array();
$array2_drug_unit=array();
while($property=mysql_fetch_row($result_drug)){
	$array2_drug[$cosa_drug]=$property[1];
	$array2_drug_unit[$cosa_drug]=$property[4];
	$cosa_drug=$cosa_drug+1;
}
//不良反应
$array_reaction=array('呕吐','抽搐','翻白眼','吐泡沫','乱跑','精神狂躁','胡言乱语','撕咬');
$count_reaction=count($array_reaction);
//施药医院
$query_hospital="SELECT * from ".C('DB_PREFIX').$tb[4];
mysql_query("SET NAMES 'UTF8'");
$result_hospital=mysql_query($query_hospital,$my_con);
$cos=mysql_num_rows($result_hospital);
$cosa_hospital=0;
while($property=mysql_fetch_row($result_hospital)){
	$array2_hospital[$cosa_hospital]=$property[1];
	$cosa_hospital=$cosa_hospital+1;
}

$timenow="'".date("Y-m-d H:i:s")."'";
$num=0;
require ('n.php');
for ($i=1;$i<$cosa;$i++){
	
	//施药人
	$b="";//名初始化
	$b=$b.getRandomName();
	
	$givedrug="'".$b."医生'";
	$drugnum="'".rand(1,10)."'";
	$use_id="'ID1000".$num."'";

	$people="'".$array2_people[rand(0,$cosa-1)]."'";
	$rand_drug=rand(0,$cosa_drug-1);
	$usedrug="'".$array2_drug[$rand_drug]."'";
	$drugunit="'".$array2_drug_unit[$rand_drug]."'";
	$reaction="'".$array_reaction[rand(0,$count_reaction-1)]."'";
	$givehospital="'".$array2_hospital[rand(0,$cosa_hospital-1)]."'";
	$value_string=$people.",".$usedrug.",".$drugnum.",".$drugunit.",".$use_id.",".$timenow.",".$reaction.",".$givedrug.",".$givehospital;
	$exec="INSERT INTO ".C("DB_PREFIX").$table." (".$zichuan.") VALUES (".$value_string.");";
	echo $exec."<br/>";

	mysql_query($exec,$my_con);
	if($erro=mysql_error()) echo $num." 次错误！！".$erro."<br/>";
	else echo $num . '次成功<br/>';
	/*
	INSERT INTO m_drug_history (people,usedrug,drugnum,drugunit,use_id,timenow,reaction,givedrug,givehospital
) VALUES ('廉奇珔','皮炎平------复方醋酸地塞米松乳膏',10,'板','ID10000','2013-07-11 03:11:07','呕吐','秆医生','中国詚聯人民医院');
	*/
	$num=$num+1;
}
echo '总共录入'.($i-1).'条数据';
mysql_close($my_con);

?>